<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-30
 * Time: 下午2:15
 */
namespace V1;
class ActionList extends \ResourceClass {
    public $isActive = true;
    public $remark = 'APP动作列表';

    public function get(array $req, array $post, &$result){
        try {
            /* 授权验证【开始】 */
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler ->getById($req['userType'],$req['uid']);
            if(!is_object($customer_cls) || $customer_cls->getEnterpriseId() ==false){
                return 'data error';
            }
            $enterprise_id = $customer_cls ->getEnterpriseId();
            if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$enterprise_id,$req['t'],$req['k']) == false) {
                return 'data illegal';
            }
            /* 授权验证【结束】 */
            $xml = file_get_contents(dirname(dirname(dirname(__FILE__))).'/config/ActionList.xml');
            $action = self::xml_to_array($xml);
            $result = $action['ActionList'];

        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
    private static function xml_to_array($xml) {
        $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
        if (preg_match_all($reg, $xml, $matches)) {
            $count = count($matches[0]);
            $arr = array();
            for ($i = 0; $i < $count; $i++) {
                $key= $matches[1][$i];
                $val = self::xml_to_array ($matches[2][$i]);  // 递归
                if (array_key_exists($key, $arr)) {
                    if (is_array($arr[$key])) {
                        if (!array_key_exists(0,$arr[$key])) {
                            $arr[$key] = array($arr[$key]);
                        }
                    } else {
                        $arr[$key] = array($arr[$key]);
                    }
                    $arr[$key][] = $val;
                } else {
                    $arr[$key] = $val;
                }
            }
            return $arr;
        } else {
            return $xml;
        }
    }
}